package com.zmn.plat.business.impl.part.category;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.google.common.base.Objects;
import com.zmn.common.dto.VtDTO;
import com.zmn.plat.business.interfaces.part.category.PartCategoryBService;
import com.zmn.plat.manager.dictionary.PartDict;
import com.zmn.plat.model.entity.part.PartCategory;
import com.zmn.plat.services.interfaces.part.PartCategoryService;

/**
 * 类描述： PartCategoryBServiceImpl
 *
 * @author DaiZhichao
 * @since 2018/10/22 11:02
 */
@Service
public class PartCategoryBServiceImpl implements PartCategoryBService {

    @Resource
    PartCategoryService partCategoryService;

    @Override
    public List<VtDTO> getCategSimpleList(Integer parentId) {

        List<VtDTO> categSimpleList = new ArrayList<>(0);
        if (parentId != null) {
            categSimpleList = partCategoryService.listByParentId(parentId, null).stream().map(partCategory -> new VtDTO(partCategory.getCategId(), partCategory.getName(), partCategory.getStatus()))
                .collect(Collectors.toList());
        }
        return categSimpleList;
    }

    @Override
    public List<PartCategory> getCategList(Integer parentId, int status) {

        List<PartCategory> partCategList = new ArrayList<>(0);

        if (parentId != null) {
            partCategList = partCategoryService.listByParentId(parentId, (short)status);
        }
        return partCategList;
    }

    @Override
    public List<VtDTO> getCategFirstSimpleList() {

        return this.getCategSimpleList(PartDict.PART_CATEGORY_ROOT_PARENTID);
    }

    @Override
    public List<VtDTO> getCategFirstEnableSimpleList() {

        return this.getCategFirstSimpleList().stream().filter(VtDTO::getStatus).collect(Collectors.toList());
    }

    @Override
    public boolean findIdenticalCategory(PartCategory partCategory) {

        List<VtDTO> partCategorySimpleList = partCategoryService.getPartCategorySimpleList(partCategory.getParentId());

        return partCategorySimpleList.stream().filter(vtDTO -> !Objects.equal(vtDTO.getValue(), partCategory.getCategId())).anyMatch(vtDTO -> Objects.equal(vtDTO.getText(), partCategory.getName()));
    }

}
